home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / binutils / srconv.c < prev    next >
C/C++ Source or Header  |  1994-08-06  |  39KB  |  1,940 lines

  1. /* srconv.c -- Sysroff conversion program
  2.    Copyright (C) 1994 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GNU Binutils.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Written by Steve Chamberlain (sac@cygnus.com)
  21.  
  22.    This program can be used to convert a coff object file
  23.    into a Hitachi OM/LM (Sysroff) format.
  24.  
  25.    All debugging information is preserved */
  26.  
  27. #include <bfd.h>
  28. #include <getopt.h>
  29. #include <stdio.h>
  30. #include <time.h>
  31. #include <libiberty.h>
  32. #include "sysroff.h"
  33. #include "coffgrok.h"
  34.  
  35. #include "coff/internal.h"
  36. #include "../bfd/libcoff.h"
  37.  
  38. #define PROGRAM_VERSION "1.5"
  39. /*#define FOOP1 1*/
  40.  
  41. static int sh;
  42. static int h8300;
  43. static void wr_cs ();
  44. static void walk_tree_scope ();
  45. static void wr_globals ();
  46.  
  47. static FILE *file;
  48. static bfd *abfd;
  49. static int debug = 0;
  50. static int quick = 0;
  51.  
  52. static struct coff_ofile *tree;
  53.  
  54. static int absolute_p;
  55. static int segmented_p;
  56. static int code;
  57.  
  58. static int ids1[20000];
  59. static int ids2[20000];
  60.  
  61. static int base1 = 0x18;
  62. static int base2 = 0x2018;
  63.  
  64. char *
  65. xcalloc (a, b)
  66.      int a;
  67.      int b;
  68. {
  69.   char *r = xmalloc (a * b);
  70.   memset (r, 0, a * b);
  71.   return r;
  72. }
  73.  
  74. static int
  75. get_member_id (x)
  76.      int x;
  77. {
  78.   if (ids2[x])
  79.     {
  80.       return ids2[x];
  81.     }
  82.   ids2[x] = base2++;
  83.   return ids2[x];
  84. }
  85.  
  86. static int
  87. get_ordinary_id (x)
  88.      int x;
  89. {
  90.   if (ids1[x])
  91.     {
  92.       return ids1[x];
  93.     }
  94.   ids1[x] = base1++;
  95.   return ids1[x];
  96. }
  97. static char *
  98. section_translate (n)
  99.      char *n;
  100. {
  101.   if (strcmp (n, ".text") == 0)
  102.     return "P";
  103.   if (strcmp (n, ".data") == 0)
  104.     return "D";
  105.   if (strcmp (n, ".bss") == 0)
  106.     return "B";
  107.   return n;
  108. }
  109.  
  110.  
  111.  
  112. #define DATE "940201073000";    /* Just a time on my birthday */
  113.  
  114.  
  115. static
  116. char *
  117. strip_suffix (name)
  118.      char *name;
  119. {
  120.   int i;
  121.   char *res;
  122.   for (i = 0; name[i] != 0 && name[i] != '.'; i++)
  123.     ;
  124.   res = (char *) xmalloc (i + 1);
  125.   memcpy (res, name, i);
  126.   res[i] = 0;
  127.   return res;
  128. }
  129.  
  130.  
  131. /* IT LEN stuff CS */
  132. static void
  133. checksum (file, ptr, size, code)
  134.      FILE *file;
  135.      char *ptr;
  136.      int size;
  137.      int code;
  138. {
  139.   int j;
  140.   int last;
  141.   int sum = 0;
  142.   int bytes = size / 8;
  143.   last = !(code & 0xff00);
  144.   if (size & 0x7)
  145.     abort ();
  146.   ptr[0] = code | (last ? 0x80 : 0);
  147.   ptr[1] = bytes + 1;
  148.  
  149.   for (j = 0; j < bytes; j++)
  150.     {
  151.       sum += ptr[j];
  152.     }
  153.   /* Glue on a checksum too */
  154.   ptr[bytes] = ~sum;
  155.   fwrite (ptr, bytes + 1, 1, file);
  156. }
  157.  
  158.  
  159.  
  160.  
  161. static void
  162. writeINT (n, ptr, idx, size, file)
  163.      int n;
  164.      char *ptr;
  165.      int *idx;
  166.      int size;
  167.      FILE *file;
  168. {
  169.   int byte = *idx / 8;
  170.  
  171.   if (size == -2)
  172.     size = 4;
  173.   if (size == -1)
  174.     size = 0;
  175.  
  176.   if (byte > 240)
  177.     {
  178.       /* Lets write out that record and do another one */
  179.       checksum (file, ptr, *idx, code | 0x1000);
  180.       *idx = 16;
  181.       byte = *idx / 8;
  182.     }
  183.   switch (size)
  184.     {
  185.     case 0:
  186.       break;
  187.     case 1:
  188.       ptr[byte] = n;
  189.       break;
  190.     case 2:
  191.       ptr[byte + 0] = n >> 8;
  192.       ptr[byte + 1] = n;
  193.       break;
  194.     case 4:
  195.       ptr[byte + 0] = n >> 24;
  196.       ptr[byte + 1] = n >> 16;
  197.       ptr[byte + 2] = n >> 8;
  198.       ptr[byte + 3] = n >> 0;
  199.       break;
  200.     default:
  201.       abort ();
  202.     }
  203.   *idx += size * 8;
  204. }
  205.  
  206.  
  207. static void
  208. writeBITS (val, ptr, idx, size)
  209.      int val;
  210.      char *ptr;
  211.      int *idx;
  212.      int size;
  213. {
  214.   int byte = *idx / 8;
  215.   int bit = *idx % 8;
  216.   int old;
  217.   *idx += size;
  218.  
  219.   old = ptr[byte];
  220.   /* Turn off all about to change bits */
  221.   old &= ~((~0 >> (8 - bit - size)) & ((1 << size) - 1));
  222.   /* Turn on the bits we want */
  223.   old |= (val & ((1 << size) - 1)) << (8 - bit - size);
  224.   ptr[byte] = old;
  225. }
  226.  
  227. static void
  228. writeBARRAY (data, ptr, idx, size, file)
  229.      barray data;
  230.      char *ptr;
  231.      int *idx;
  232.      int size;
  233.      FILE *file;
  234. {
  235.   int i;
  236.   writeINT (data.len, ptr, idx, 1, file);
  237.   for (i = 0; i < data.len; i++)
  238.     {
  239.       writeINT (data.data[i], ptr, idx, 1, file);
  240.     }
  241.  
  242.  
  243.  
  244.  
  245. }
  246. static void
  247. writeCHARS (string, ptr, idx, size, file)
  248.      char *string;
  249.      char *ptr;
  250.      int *idx;
  251.      int size;
  252.      FILE *file;
  253. {
  254.   int i = *idx / 8;
  255.  
  256.   if (i > 240)
  257.     {
  258.       /* Lets write out that record and do another one */
  259.       checksum (file, ptr, *idx, code | 0x1000);
  260.       *idx = 16;
  261.       i = *idx / 8;
  262.     }
  263.  
  264.   if (size == 0)
  265.     {
  266.       /* Variable length string */
  267.       size = strlen (string);
  268.       ptr[i++] = size;
  269.     }
  270.  
  271.  
  272.   memcpy (ptr + i, string, size);
  273.   i += size;
  274.   *idx = i * 8;
  275. }
  276.  
  277. #define SYSROFF_SWAP_OUT
  278. #include "sysroff.c"
  279.  
  280.  
  281. static char *rname_sh[] =
  282. {
  283.   "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
  284. };
  285.  
  286. static char *rname_h8300[] =
  287. {
  288.   "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
  289. };
  290.  
  291. static void
  292. wr_tr ()
  293. {
  294.   struct IT_tr t;
  295.   sysroff_swap_tr_out (file, &t);
  296. }
  297.  
  298. static void
  299. wr_un (ptr, sfile, first)
  300.      struct coff_ofile *ptr;
  301.      struct coff_sfile *sfile;
  302.      int first;
  303. {
  304.   struct IT_un un;
  305.  
  306.   struct coff_symbol *s;
  307.  
  308.   un.spare1 = 0;
  309.  
  310.   if (abfd->flags & EXEC_P)
  311.     un.format = FORMAT_LM;
  312.   else
  313.     un.format = FORMAT_OM;
  314.   un.spare1 = 0;
  315.   un.nsections = ptr->nsections - 1;    /*  Don't count the abs section */
  316.   un.nextdefs = 0;
  317.   un.nextrefs = 0;
  318.   /* Count all the undefined and defined variables with global scope */
  319.  
  320.   if (first)
  321.     {
  322.       for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
  323.     {
  324.       if (s->visible->type == coff_vis_ext_def
  325.           || s->visible->type == coff_vis_common)
  326.         un.nextdefs++;
  327.  
  328.       if (s->visible->type == coff_vis_ext_ref)
  329.         un.nextrefs++;
  330.     }
  331.     }
  332.   if (sh)
  333.     {
  334.       un.tool = "C_SH";
  335.     }
  336.   if (h8300)
  337.     {
  338.       un.tool = "C_H8/300H";
  339.     }
  340.   un.tcd = DATE;
  341.   un.linker = "L_GX00";
  342.   un.lcd = DATE;
  343.   un.name = sfile->name;
  344.   sysroff_swap_un_out (file, &un);
  345. }
  346.  
  347.  
  348. static void
  349. wr_hd (p)
  350.      struct coff_ofile *p;
  351. {
  352.   struct IT_hd hd;
  353.  
  354.   hd.spare1 = 0;
  355.   if (abfd->flags & EXEC_P)
  356.     {
  357.       hd.mt = MTYPE_ABS_LM;
  358.     }
  359.   else
  360.     {
  361.       hd.mt = MTYPE_OMS_OR_LMS;
  362.     }
  363.   hd.cd = DATE;
  364.  
  365.   hd.nu = p->nsources;        /* Always one unit */
  366.   hd.code = 0;            /* Always ASCII */
  367.   hd.ver = "0200";        /* Version 2.00 */
  368.   switch (abfd->arch_info->arch)
  369.     {
  370.     case bfd_arch_h8300:
  371.       hd.au = 8;
  372.       hd.si = 32;
  373.       hd.afl = 2;
  374.       hd.spcsz = 0;
  375.       hd.segsz = 0;
  376.       hd.segsh = 0;
  377.       hd.cpu = "H8300H";
  378.       h8300 = 1;
  379.       break;
  380.     case bfd_arch_sh:
  381.       hd.au = 8;
  382.       hd.si = 32;
  383.       hd.afl = 2;
  384.       hd.spcsz = 0;
  385.       hd.segsz = 0;
  386.       hd.segsh = 0;
  387.       hd.cpu = "SH";
  388.       sh = 1;
  389.       break;
  390.     }
  391.  
  392.   if (!abfd->flags & EXEC_P)
  393.     {
  394.       hd.ep = 0;
  395.     }
  396.   else
  397.     {
  398.       hd.ep = 1;
  399.       hd.uan = 0;
  400.       hd.sa = 0;
  401.       hd.sad = 0;
  402.       hd.address = bfd_get_start_address (abfd);
  403.     }
  404.  
  405.   hd.os = "";
  406.   hd.sys = "";
  407.   hd.mn = strip_suffix (abfd->filename);
  408.  
  409.  
  410.   sysroff_swap_hd_out (file, &hd);
  411. }
  412.  
  413.  
  414. static void
  415. wr_sh (p, sec)
  416.      struct coff_ofile *p;
  417.      struct coff_section *sec;
  418. {
  419.   struct IT_sh sh;
  420.   sh.unit = 0;
  421.   sh.section = sec->number;
  422. #ifdef FOOP1
  423.   sh.section = 0;
  424. #endif
  425.   sysroff_swap_sh_out (file, &sh);
  426. }
  427.  
  428.  
  429. static void
  430. wr_ob (p, section)
  431.      struct coff_ofile *p;
  432.      struct coff_section *section;
  433. {
  434.   int i;
  435.   int first = 1;
  436.   unsigned char stuff[200];
  437.  
  438.   i = 0;
  439.   while (i < section->size)
  440.     {
  441.       struct IT_ob ob;
  442.       int todo = 200;        /* Copy in 200 byte lumps */
  443.       ob.spare = 0;
  444.       if (i + todo > section->size)
  445.     todo = section->size - i;
  446.  
  447.       if (first)
  448.     {
  449.       ob.saf = 1;
  450.       if (abfd->flags & EXEC_P)
  451.         ob.address = section->address;
  452.       else
  453.         ob.address = 0;
  454.  
  455.       first = 0;
  456.     }
  457.       else
  458.     {
  459.       ob.saf = 0;
  460.     }
  461.  
  462.       ob.cpf = 0;        /* Never compress */
  463.       ob.data.len = todo;
  464.       bfd_get_section_contents (abfd, section->bfd_section, stuff, i, todo);
  465.       ob.data.data = stuff;
  466.       sysroff_swap_ob_out (file, &ob /*, i + todo < section->size*/ );
  467.       i += todo;
  468.     }
  469. }
  470.  
  471. static void
  472. wr_rl (ptr, sec)
  473.      struct coff_ofile *ptr;
  474.      struct coff_section *sec;
  475. {
  476.   int nr = sec->nrelocs;
  477.   int i;
  478.   for (i = 0; i < nr; i++)
  479.     {
  480.       struct coff_reloc *r = sec->relocs + i;
  481.       struct coff_symbol *ref;
  482.       struct IT_rl rl;
  483.       rl.apol = 0;
  484.       rl.boundary = 0;
  485.       rl.segment = 1;
  486.       rl.sign = 0;
  487.       rl.check = 0;
  488.       rl.addr = r->offset;
  489.       rl.bitloc = 0;
  490.       rl.flen = 32;        /* SH Specific */
  491.       /* What sort of reloc ? Look in the section to find out */
  492.       ref = r->symbol;
  493.       if (ref->visible->type == coff_vis_ext_ref)
  494.     {
  495.       rl.bcount = 4;    /* Always 4 for us */
  496.       rl.op = OP_EXT_REF;
  497.       rl.symn = ref->er_number;
  498.     }
  499.       else if (ref->visible->type == coff_vis_common)
  500.     {
  501.       rl.bcount = 11;    /* Always 11 for us */
  502.       rl.op = OP_SEC_REF;
  503.       rl.secn = ref->where->section->number;
  504.       rl.copcode_is_3 = 3;
  505.       rl.alength_is_4 = 4;
  506.       rl.addend = ref->where->offset - ref->where->section->address;
  507.       rl.aopcode_is_0x20 = 0x20;
  508.     }
  509.  
  510.       else
  511.     {
  512.       rl.bcount = 11;    /* Always 11 for us */
  513.       rl.op = OP_SEC_REF;
  514.       rl.secn = ref->where->section->number;
  515.       rl.copcode_is_3 = 3;
  516.       rl.alength_is_4 = 4;
  517.       rl.addend = -ref->where->section->address;
  518.       rl.aopcode_is_0x20 = 0x20;
  519.     }
  520.       rl.end = 0xff;
  521.       if (rl.op == OP_SEC_REF
  522.       || rl.op == OP_EXT_REF)
  523.     {
  524.       sysroff_swap_rl_out (file, &rl);
  525.     }
  526.     }
  527. }
  528.  
  529. static void
  530. wr_object_body (p)
  531.      struct coff_ofile *p;
  532. {
  533.   int i;
  534.   for (i = 1; i < p->nsections; i++)
  535.     {
  536.       wr_sh (p, p->sections + i);
  537.       wr_ob (p, p->sections + i);
  538.       wr_rl (p, p->sections + i);
  539.     }
  540. }
  541.  
  542. static void
  543. wr_dps_start (sfile, section, scope, type, nest)
  544.      struct coff_sfile *sfile;
  545.      struct coff_section *section;
  546.      struct coff_scope *scope;
  547.      int type;
  548. {
  549.   struct IT_dps dps;
  550.   dps.end = 0;
  551.   dps.opt = 0;
  552.   dps.type = type;
  553.   if (scope->sec)
  554.     {
  555.       dps.san = scope->sec->number;
  556.       dps.address = scope->offset - find_base (sfile, scope->sec);
  557.       dps.block_size = scope->size;
  558.       if (debug)
  559.     {
  560.       printf ("DPS %s %d %x\n",
  561.           sfile->name,
  562.           nest,
  563.           dps.address);
  564.  
  565.     }
  566.     }
  567.   else
  568.     {
  569.       dps.san = 0;
  570.       dps.address = 0;
  571.       dps.block_size = 0;
  572.     }
  573.  
  574.   dps.nesting = nest;
  575.   dps.neg = 0x1001;
  576.   sysroff_swap_dps_out (file, &dps);
  577. }
  578.  
  579. static void
  580. wr_dps_end (section, scope, type)
  581.      struct coff_section *section;
  582.      struct coff_scope *scope;
  583.      int type;
  584. {
  585.   struct IT_dps dps;
  586.   dps.end = 1;
  587.   dps.type = type;
  588.   sysroff_swap_dps_out (file, &dps);
  589. }
  590.  
  591. static int *
  592. nints (x)
  593.      int x;
  594. {
  595.   return (int *) (xcalloc (sizeof (int), x));
  596. }
  597.  
  598. static void walk_tree_symbol ();
  599. static void
  600. walk_tree_type_1 (sfile, symbol, type, nest)
  601.      struct coff_sfile *sfile;
  602.      struct coff_symbol *symbol;
  603.      struct coff_type *type;
  604.      int nest;
  605. {
  606.   switch (type->type)
  607.     {
  608.     case coff_secdef_type:
  609.     case coff_basic_type:
  610.       {
  611.     struct IT_dbt dbt;
  612.  
  613.     switch (type->u.basic)
  614.       {
  615.       case T_NULL:
  616.       case T_VOID:
  617.         dbt.btype = BTYPE_VOID;
  618.         dbt.sign = BTYPE_UNSPEC;
  619.         dbt.fptype = FPTYPE_NOTSPEC;
  620.         break;
  621.       case T_CHAR:
  622.         dbt.btype = BTYPE_CHAR;
  623.         dbt.sign = BTYPE_UNSPEC;
  624.         dbt.fptype = FPTYPE_NOTSPEC;
  625.         break;
  626.       case T_SHORT:
  627.       case T_INT:
  628.       case T_LONG:
  629.         dbt.btype = BTYPE_INT;
  630.         dbt.sign = SIGN_SIGNED;
  631.         dbt.fptype = FPTYPE_NOTSPEC;
  632.         break;
  633.       case T_FLOAT:
  634.         dbt.btype = BTYPE_FLOAT;
  635.         dbt.fptype = FPTYPE_SINGLE;
  636.         break;
  637.       case T_DOUBLE:
  638.         dbt.btype = BTYPE_FLOAT;
  639.         dbt.fptype = FPTYPE_DOUBLE;
  640.         break;
  641.       case T_LNGDBL:
  642.         dbt.btype = BTYPE_FLOAT;
  643.         dbt.fptype = FPTYPE_EXTENDED;
  644.         break;
  645.       case T_UCHAR:
  646.         dbt.btype = BTYPE_CHAR;
  647.         dbt.sign = SIGN_UNSIGNED;
  648.         dbt.fptype = FPTYPE_NOTSPEC;
  649.         break;
  650.       case T_USHORT:
  651.       case T_UINT:
  652.       case T_ULONG:
  653.         dbt.btype = BTYPE_INT;
  654.         dbt.sign = SIGN_UNSIGNED;
  655.         dbt.fptype = FPTYPE_NOTSPEC;
  656.         break;
  657.       }
  658.     dbt.bitsize = type->size;
  659.     dbt.neg = 0x1001;
  660.     sysroff_swap_dbt_out (file, &dbt);
  661.     break;
  662.       }
  663.     case coff_pointer_type:
  664.       {
  665.     struct IT_dpt dpt;
  666.     walk_tree_type_1 (sfile, symbol, type->u.pointer.points_to, nest + 1);
  667.     dpt.neg = 0x1001;
  668.     sysroff_swap_dpt_out (file, &dpt);
  669.     break;
  670.       }
  671.  
  672.     case coff_function_type:
  673.       {
  674.     struct IT_dfp dfp;
  675.     struct coff_symbol *param;
  676.     dfp.end = 0;
  677.     dfp.spare = 0;
  678.     dfp.nparams = type->u.function.parameters->nvars;
  679.     dfp.neg = 0x1001;
  680.  
  681.     walk_tree_type_1 (sfile, symbol, type->u.function.function_returns, nest + 1);
  682.  
  683.     sysroff_swap_dfp_out (file, &dfp);
  684.  
  685.     for (param = type->u.function.parameters->vars_head;
  686.          param;
  687.          param = param->next)
  688.       {
  689.         walk_tree_symbol (sfile, 0, param, nest);
  690.       }
  691.     dfp.end = 1;
  692.     sysroff_swap_dfp_out (file, &dfp);
  693.     break;
  694.       }
  695.  
  696.     case coff_structdef_type:
  697.       {
  698.     struct IT_dbt dbt;
  699.     struct IT_dds dds;
  700.     struct coff_symbol *member;
  701.     dds.spare = 0;
  702.     dbt.btype = BTYPE_STRUCT;
  703.     dbt.bitsize = type->size;
  704.     dbt.sign = SIGN_UNSPEC;
  705.     dbt.fptype = FPTYPE_NOTSPEC;
  706.     dbt.sid = get_member_id (type->u.astructdef.idx);
  707.     dbt.neg = 0x1001;
  708.     sysroff_swap_dbt_out (file, &dbt);
  709.     dds.end = 0;
  710.     dds.neg = 0x1001;
  711.     sysroff_swap_dds_out (file, &dds);
  712.     for (member = type->u.astructdef.elements->vars_head;
  713.          member;
  714.          member = member->next)
  715.       {
  716.         walk_tree_symbol (sfile, 0, member, nest + 1);
  717.       }
  718.  
  719.     dds.end = 1;
  720.     sysroff_swap_dds_out (file, &dds);
  721.  
  722.       }
  723.       break;
  724.     case coff_structref_type:
  725.       {
  726.     struct IT_dbt dbt;
  727.     dbt.btype = BTYPE_TAG;
  728.     dbt.bitsize = type->size;
  729.     dbt.sign = SIGN_UNSPEC;
  730.     dbt.fptype = FPTYPE_NOTSPEC;
  731.     if (type->u.astructref.ref)
  732.       {
  733.         dbt.sid = get_member_id (type->u.astructref.ref->number);
  734.       }
  735.     else
  736.       {
  737.         dbt.sid = 0;
  738.       }
  739.  
  740.     dbt.neg = 0x1001;
  741.     sysroff_swap_dbt_out (file, &dbt);
  742.       }
  743.       break;
  744.     case coff_array_type:
  745.       {
  746.     struct IT_dar dar;
  747.     int j;
  748.     int dims = 1;        /* Only output one dimension at a time */
  749.     dar.dims = dims;
  750.     dar.variable = nints (dims);
  751.     dar.subtype = nints (dims);
  752.     dar.spare = nints (dims);
  753.     dar.max_variable = nints (dims);
  754.     dar.maxspare = nints (dims);
  755.     dar.max = nints (dims);
  756.     dar.min_variable = nints (dims);
  757.     dar.min = nints (dims);
  758.     dar.minspare = nints (dims);
  759.     dar.neg = 0x1001;
  760.     dar.length = type->size / type->u.array.dim;
  761.     for (j = 0; j < dims; j++)
  762.       {
  763.         dar.variable[j] = VARIABLE_FIXED;
  764.         dar.subtype[j] = SUB_INTEGER;
  765.         dar.spare[j] = 0;
  766.         dar.max_variable[j] = 0;
  767.         dar.max[j] = type->u.array.dim;
  768.         dar.min_variable[j] = 0;
  769.         dar.min[j] = 1;    /* Why isn't this 0 ? */
  770.       }
  771.     walk_tree_type_1 (sfile, symbol, type->u.array.array_of, nest + 1);
  772.     sysroff_swap_dar_out (file, &dar);
  773.       }
  774.       break;
  775.     case coff_enumdef_type:
  776.       {
  777.     struct IT_dbt dbt;
  778.     struct IT_den den;
  779.     struct coff_symbol *member;
  780.     dbt.btype = BTYPE_ENUM;
  781.     dbt.bitsize = type->size;
  782.     dbt.sign = SIGN_UNSPEC;
  783.     dbt.fptype = FPTYPE_NOTSPEC;
  784.     dbt.sid = get_member_id (type->u.aenumdef.idx);
  785.     dbt.neg = 0x1001;
  786.     sysroff_swap_dbt_out (file, &dbt);
  787.  
  788.     den.end = 0;
  789.     den.neg = 0x1001;
  790.     den.spare = 0;
  791.     sysroff_swap_den_out (file, &den);
  792.     for (member = type->u.aenumdef.elements->vars_head;
  793.          member;
  794.          member = member->next)
  795.       {
  796.         walk_tree_symbol (sfile, 0, member, nest + 1);
  797.       }
  798.  
  799.     den.end = 1;
  800.     sysroff_swap_den_out (file, &den);
  801.       }
  802.       break;
  803.  
  804.       break;
  805.     case coff_enumref_type:
  806.       {
  807.     struct IT_dbt dbt;
  808.     dbt.btype = BTYPE_TAG;
  809.     dbt.bitsize = type->size;
  810.     dbt.sign = SIGN_UNSPEC;
  811.     dbt.fptype = FPTYPE_NOTSPEC;
  812.     dbt.sid = get_member_id (type->u.aenumref.ref->number);
  813.     dbt.neg = 0x1001;
  814.     sysroff_swap_dbt_out (file, &dbt);
  815.       }
  816.       break;
  817.     default:
  818.       abort ();
  819.     }
  820. }
  821.  
  822. static void
  823. dty_start ()
  824. {
  825.   struct IT_dty dty;
  826.   dty.end = 0;
  827.   dty.neg = 0x1001;
  828.   dty.spare = 0;
  829.   sysroff_swap_dty_out (file, &dty);
  830. }
  831.  
  832. static void
  833. dty_stop ()
  834. {
  835.   struct IT_dty dty;
  836.   dty.end = 0;
  837.   dty.neg = 0x1001;
  838.   dty.end = 1;
  839.   sysroff_swap_dty_out (file, &dty);
  840. }
  841.  
  842.  
  843. static void
  844. dump_tree_structure (sfile, symbol, type, nest)
  845.      struct coff_sfile *sfile;
  846.      struct coff_symbol *symbol;
  847.      struct coff_type *type;
  848.      int nest;
  849. {
  850.   if (symbol->type->type == coff_function_type)
  851.     {
  852.  
  853.  
  854.     }
  855.  
  856. }
  857.  
  858.  
  859. static void
  860. walk_tree_type (sfile, symbol, type, nest)
  861.  
  862.      struct
  863.      coff_sfile *sfile;
  864.      struct coff_symbol *symbol;
  865.      struct coff_type *type;
  866.      int nest;
  867. {
  868.   if (symbol->type->type == coff_function_type)
  869.     {
  870.  
  871.       struct IT_dty dty;
  872.       dty.end = 0;
  873.       dty.neg = 0x1001;
  874.  
  875.       sysroff_swap_dty_out (file, &dty);
  876.       walk_tree_type_1 (sfile, symbol, type, nest);
  877.       dty.end = 1;
  878.       sysroff_swap_dty_out (file, &dty);
  879.  
  880.       wr_dps_start (sfile,
  881.             symbol->where->section,
  882.             symbol->type->u.function.code,
  883.             BLOCK_TYPE_FUNCTION, nest);
  884.       wr_dps_start (sfile, symbol->where->section,
  885.             symbol->type->u.function.code,
  886.             BLOCK_TYPE_BLOCK, nest);
  887.       walk_tree_scope (symbol->where->section,
  888.                sfile,
  889.                symbol->type->u.function.code,
  890.                nest + 1, BLOCK_TYPE_BLOCK);
  891.  
  892.       wr_dps_end (symbol->where->section,
  893.           symbol->type->u.function.code,
  894.           BLOCK_TYPE_BLOCK);
  895.       wr_dps_end (symbol->where->section,
  896.           symbol->type->u.function.code, BLOCK_TYPE_FUNCTION);
  897.  
  898.     }
  899.   else
  900.     {
  901.       struct IT_dty dty;
  902.       dty.end = 0;
  903.       dty.neg = 0x1001;
  904.       sysroff_swap_dty_out (file, &dty);
  905.       walk_tree_type_1 (sfile, symbol, type, nest);
  906.       dty.end = 1;
  907.       sysroff_swap_dty_out (file, &dty);
  908.     }
  909.  
  910. }
  911.  
  912.  
  913.  
  914. static void
  915. walk_tree_symbol (sfile, section, symbol, nest)
  916.      struct coff_sfile *sfile;
  917.      struct coff_section *section;
  918.      struct coff_symbol *symbol;
  919.      int nest;
  920. {
  921.   struct IT_dsy dsy;
  922.  
  923.   dsy.spare2 = 0;
  924.   dsy.nesting = nest;
  925.  
  926.   switch (symbol->type->type)
  927.     {
  928.     case coff_function_type:
  929.       dsy.type = STYPE_FUNC;
  930.       dsy.assign = 1;
  931.       break;
  932.     case coff_structref_type:
  933.     case coff_pointer_type:
  934.     case coff_array_type:
  935.     case coff_basic_type:
  936.     case coff_enumref_type:
  937.       dsy.type = STYPE_VAR;
  938.       dsy.assign = 1;
  939.       break;
  940.     case coff_enumdef_type:
  941.       dsy.type = STYPE_TAG;
  942.       dsy.assign = 0;
  943.       dsy.magic = 2;
  944.       break;
  945.     case coff_structdef_type:
  946.       dsy.type = STYPE_TAG;
  947.       dsy.assign = 0;
  948.       dsy.magic = symbol->type->u.astructdef.isstruct ? 0 : 1;
  949.       break;
  950.     case coff_secdef_type:
  951.       return;
  952.     default:
  953.       abort ();
  954.     }
  955.  
  956.   if (symbol->where->where == coff_where_member_of_struct)
  957.     {
  958.       dsy.assign = 0;
  959.       dsy.type = STYPE_MEMBER;
  960.     }
  961.   if (symbol->where->where == coff_where_member_of_enum)
  962.     {
  963.       dsy.type = STYPE_ENUM;
  964.       dsy.assign = 0;
  965.       dsy.vallen = 4;
  966.       dsy.value = symbol->where->offset;
  967.     }
  968.  
  969.   if (symbol->type->type == coff_structdef_type
  970.       || symbol->where->where == coff_where_entag
  971.       || symbol->where->where == coff_where_strtag)
  972.     {
  973.       dsy.snumber = get_member_id (symbol->number);
  974.     }
  975.   else
  976.     {
  977.       dsy.snumber = get_ordinary_id (symbol->number);
  978.     }
  979.  
  980.  
  981.   dsy.sname = symbol->name[0] == '_' ? symbol->name + 1 : symbol->name;
  982.  
  983.   switch (symbol->visible->type)
  984.     {
  985.     case coff_vis_common:
  986.     case coff_vis_ext_def:
  987.       dsy.ainfo = AINFO_STATIC_EXT_DEF;
  988.       break;
  989.     case coff_vis_ext_ref:
  990.       dsy.ainfo = AINFO_STATIC_EXT_REF;
  991.       break;
  992.     case coff_vis_int_def:
  993.       dsy.ainfo = AINFO_STATIC_INT;
  994.       break;
  995.     case coff_vis_auto:
  996.     case coff_vis_autoparam:
  997.       dsy.ainfo = AINFO_AUTO;
  998.       break;
  999.     case coff_vis_register:
  1000.     case coff_vis_regparam:
  1001.       dsy.ainfo = AINFO_REG;
  1002.       break;
  1003.       break;
  1004.     case coff_vis_tag:
  1005.     case coff_vis_member_of_struct:
  1006.     case coff_vis_member_of_enum:
  1007.       break;
  1008.     default:
  1009.       abort ();
  1010.     }
  1011.  
  1012.   dsy.dlength = symbol->type->size;
  1013.   switch (symbol->where->where)
  1014.     {
  1015.     case coff_where_memory:
  1016.  
  1017.       dsy.section = symbol->where->section->number;
  1018. #ifdef FOOP
  1019.       dsy.section = 0;
  1020. #endif
  1021.       break;
  1022.     case coff_where_member_of_struct:
  1023.     case coff_where_member_of_enum:
  1024.     case coff_where_stack:
  1025.     case coff_where_register:
  1026.     case coff_where_unknown:
  1027.     case coff_where_strtag:
  1028.  
  1029.     case coff_where_entag:
  1030.     case coff_where_typedef:
  1031.       break;
  1032.     default:
  1033.       abort ();
  1034.     }
  1035.  
  1036.   switch (symbol->where->where)
  1037.     {
  1038.     case coff_where_memory:
  1039.       dsy.address = symbol->where->offset - find_base (sfile, symbol->where->section);
  1040.       break;
  1041.     case coff_where_stack:
  1042.       dsy.address = symbol->where->offset;
  1043.       break;
  1044.     case coff_where_member_of_struct:
  1045.  
  1046.  
  1047.       if (symbol->where->bitsize)
  1048.     {
  1049.       int bits = (symbol->where->offset * 8 + symbol->where->bitoffset);
  1050.       dsy.bitunit = 1;
  1051.       dsy.field_len = symbol->where->bitsize;
  1052.       dsy.field_off = (bits / 32) * 4;
  1053.       dsy.field_bitoff = bits % 32;
  1054.     }
  1055.       else
  1056.     {
  1057.       dsy.bitunit = 0;
  1058.  
  1059.       dsy.field_len = symbol->type->size;
  1060.       dsy.field_off = symbol->where->offset;
  1061.     }
  1062.       break;
  1063.     case coff_where_member_of_enum:
  1064.       /*      dsy.bitunit = 0;
  1065.         dsy.field_len  = symbol->type->size;
  1066.         dsy.field_off = symbol->where->offset;*/
  1067.       break;
  1068.     case coff_where_register:
  1069.     case coff_where_unknown:
  1070.     case coff_where_strtag:
  1071.  
  1072.     case coff_where_entag:
  1073.     case coff_where_typedef:
  1074.       break;
  1075.     default:
  1076.       abort ();
  1077.     }
  1078.  
  1079.   if (symbol->where->where == coff_where_register)
  1080.     {
  1081.       if (sh)
  1082.     dsy.reg = rname_sh[symbol->where->offset];
  1083.       if (h8300)
  1084.     dsy.reg = rname_h8300[symbol->where->offset];
  1085.     }
  1086.  
  1087.   switch (symbol->visible->type)
  1088.     {
  1089.     case coff_vis_common:
  1090.       /* We do this 'cause common C symbols are treated as extdefs */
  1091.     case coff_vis_ext_def:
  1092.     case coff_vis_ext_ref:
  1093.  
  1094.       dsy.ename = symbol->name;
  1095.       break;
  1096.  
  1097.     case coff_vis_regparam:
  1098.     case coff_vis_autoparam:
  1099.       dsy.type = STYPE_PARAMETER;
  1100.       break;
  1101.  
  1102.     case coff_vis_int_def:
  1103.  
  1104.     case coff_vis_auto:
  1105.     case coff_vis_register:
  1106.     case coff_vis_tag:
  1107.     case coff_vis_member_of_struct:
  1108.     case coff_vis_member_of_enum:
  1109.       break;
  1110.     default:
  1111.       abort ();
  1112.     }
  1113.  
  1114.   dsy.sfn = 0;
  1115.   dsy.sln = 2;
  1116.  
  1117.   dsy.neg = 0x1001;
  1118.  
  1119.  
  1120.   sysroff_swap_dsy_out (file, &dsy);
  1121.  
  1122.   walk_tree_type (sfile, symbol, symbol->type, nest);
  1123. }
  1124.  
  1125.  
  1126. static void
  1127. walk_tree_scope (section, sfile, scope, nest, type)
  1128.      struct coff_section *section;
  1129.      struct coff_sfile *sfile;
  1130.      struct coff_scope *scope;
  1131.      int nest;
  1132.      int type;
  1133. {
  1134.   struct coff_symbol *vars;
  1135.   struct coff_scope *child;
  1136.  
  1137.   if (scope->vars_head
  1138.       || (scope->list_head && scope->list_head->vars_head))
  1139.     {
  1140.       wr_dps_start (sfile, section, scope, type, nest);
  1141.  
  1142.       if (nest == 0)
  1143.     wr_globals (tree, sfile, nest + 1);
  1144.  
  1145.       for (vars = scope->vars_head; vars; vars = vars->next)
  1146.     {
  1147.       walk_tree_symbol (sfile, section, vars, nest);
  1148.     }
  1149.  
  1150.       for (child = scope->list_head; child; child = child->next)
  1151.     {
  1152.       walk_tree_scope (section, sfile, child, nest + 1, BLOCK_TYPE_BLOCK);
  1153.     }
  1154.  
  1155.       wr_dps_end (section, scope, type);
  1156.     }
  1157. }
  1158. static void
  1159. walk_tree_sfile (section, sfile)
  1160.      struct coff_section *section;
  1161.      struct coff_sfile *sfile;
  1162. {
  1163.   walk_tree_scope (section, sfile, sfile->scope, 0, BLOCK_TYPE_COMPUNIT);
  1164.  
  1165. }
  1166.  
  1167. static void
  1168. wr_program_structure (p, sfile)
  1169.      struct coff_ofile *p;
  1170.      struct coff_sfile *sfile;
  1171. {
  1172.  
  1173.   walk_tree_sfile (p->sections + 4, sfile);
  1174.  
  1175. }
  1176.  
  1177. static void
  1178. wr_du (p, sfile, n)
  1179.      struct coff_ofile *p;
  1180.      struct coff_sfile *sfile;
  1181.      int n;
  1182. {
  1183.   struct IT_du du;
  1184.   int lim;
  1185.   struct coff_symbol *symbol;
  1186.   static int incit = 0x500000;
  1187.   int i;
  1188.   int j;
  1189.   int used = 0;
  1190.   unsigned int *lowest = (unsigned *) nints (p->nsections);
  1191.   unsigned int *highest = (unsigned *) nints (p->nsections);
  1192.   du.spare = 0;
  1193.   du.format = abfd->flags & EXEC_P ? 0 : 1;
  1194.   du.optimized = 0;
  1195.   du.unit = n;
  1196.   du.sections = p->nsections - 1;
  1197.   du.san = (int *) xcalloc (sizeof (int), du.sections);
  1198.   du.address = nints (du.sections);
  1199.   du.length = nints (du.sections);
  1200.  
  1201.   for (i = 0; i < du.sections; i++)
  1202.     {
  1203.       lowest[i] = ~0;
  1204.       highest[i] = 0;
  1205.     }
  1206.  
  1207.   /* Look through all the symbols and try and work out the extents in this
  1208.      source file */
  1209. #if 0
  1210.   for (symbol = sfile->scope->vars_head;
  1211.        symbol;
  1212.        symbol = symbol->next)
  1213.     {
  1214.       if (symbol->type->type == coff_secdef_type)
  1215.     {
  1216.       unsigned int low = symbol->where->offset;
  1217.       unsigned int high = symbol->where->offset + symbol->type->size - 1;
  1218.       struct coff_section *section = symbol->where->section;
  1219.  
  1220.       int sn = section->number;
  1221.       if (low < lowest[sn])
  1222.         lowest[sn] = low;
  1223.       if (high > highest[sn])
  1224.         highest[sn] = high;
  1225.     }
  1226.     }
  1227.  
  1228.  
  1229.   for (i = 0; i < du.sections; i++)
  1230.     {
  1231.       if (highest[i] == 0)
  1232.     {
  1233.       lowest[i] = highest[i] = incit;
  1234.     }
  1235.       du.san[used] = i;
  1236.       du.length[used] = highest[i] - lowest[i];
  1237.       du.address[used] = abfd->flags & EXEC_P ? lowest[i] : 0;
  1238.       if (debug)
  1239.     {
  1240.       printf (" section %6s 0x%08x..0x%08x\n",
  1241.           p->sections[i + 1].name,
  1242.           lowest[i],
  1243.           highest[i]);
  1244.     }
  1245.       used++;
  1246.     }
  1247.  
  1248. #endif
  1249.   lim = du.sections;
  1250.   for (j = 0; j < lim; j++)
  1251.     {
  1252.       int src = j;
  1253.       int dst = j;
  1254.       du.san[dst] = dst;
  1255.       if (sfile->section[src].init)
  1256.     {
  1257.       du.length[dst]
  1258.         = sfile->section[src].high - sfile->section[src].low + 1;
  1259.       du.address[dst]
  1260.         = sfile->section[src].low;
  1261.     }
  1262.       else
  1263.     {
  1264.       du.length[dst] = 0;
  1265.       du.address[dst] = 0;
  1266.     }
  1267.       if (debug)
  1268.     {
  1269.       if (sfile->section[src].parent)
  1270.         {
  1271.           printf (" section %6s 0x%08x..0x%08x\n",
  1272.               sfile->section[src].parent->name,
  1273.               du.address[dst],
  1274.               du.address[dst] + du.length[dst] - 1);
  1275.         }
  1276.     }
  1277.       du.sections = dst + 1;
  1278.     }
  1279.  
  1280.   du.tool = "c_gcc";
  1281.   du.date = DATE;
  1282.  
  1283.   sysroff_swap_du_out (file, &du);
  1284. }
  1285.  
  1286. static void
  1287. wr_dus (p, sfile)
  1288.      struct coff_ofile *p;
  1289.      struct coff_sfile *sfile;
  1290. {
  1291.  
  1292.   struct IT_dus dus;
  1293.   int i;
  1294.  
  1295.   dus.efn = 0x1001;
  1296.   dus.ns = 1;            /* p->nsources; sac 14 jul 94 */
  1297.   dus.drb = nints (dus.ns);
  1298.   dus.fname = (char **) xcalloc (sizeof (char *), dus.ns);
  1299.   dus.spare = nints (dus.ns);
  1300.   dus.ndir = 0;
  1301.   /* Find the filenames */
  1302. #if 0
  1303.   i = 0;
  1304.  
  1305.   for (sfile = p->source_head;
  1306.        sfile;
  1307.        sfile = sfile->next)
  1308.     {
  1309.       dus.drb[i] = 0;
  1310.       dus.spare[i] = 0;
  1311.       dus.fname[i] = sfile->name;
  1312.       i++;
  1313.     }
  1314. #else
  1315.   dus.drb[0] = 0;
  1316.   dus.fname[0] = sfile->name;
  1317. #endif
  1318.  
  1319.   sysroff_swap_dus_out (file, &dus);
  1320.  
  1321. }
  1322.  
  1323. /* Find the offset of the .text section for this sfile in the
  1324.     .text section for the output file */
  1325.  
  1326. static int
  1327. find_base (sfile, section)
  1328.      struct coff_sfile *sfile;
  1329.      struct coff_section *section;
  1330. {
  1331.   return sfile->section[section->number].low;
  1332. }
  1333. static void
  1334. wr_dln (p, sfile, n)
  1335.      struct coff_ofile *p;
  1336.      struct coff_sfile *sfile;
  1337.      int n;
  1338.  
  1339. {
  1340. #if 0
  1341.   if (n == 0)
  1342.     {
  1343.       /* Count up all the linenumbers */
  1344.       struct coff_symbol *sy;
  1345.       int lc = 0;
  1346.       struct IT_dln dln;
  1347.  
  1348.       int idx;
  1349.  
  1350.       for (sy = p->symbol_list_head;
  1351.        sy;
  1352.        sy = sy->next_in_ofile_list)
  1353.     {
  1354.       struct coff_type *t = sy->type;
  1355.       if (t->type == coff_function_type)
  1356.         {
  1357.           struct coff_line *l = t->u.function.lines;
  1358.           lc += l->nlines;
  1359.         }
  1360.     }
  1361.  
  1362.       dln.sfn = nints (lc);
  1363.       dln.sln = nints (lc);
  1364.       dln.lln = nints (lc);
  1365.       dln.section = nints (lc);
  1366.  
  1367.       dln.from_address = nints (lc);
  1368.       dln.to_address = nints (lc);
  1369.  
  1370.  
  1371.       dln.neg = 0x1001;
  1372.  
  1373.       dln.nln = lc;
  1374.  
  1375.       /* Run through once more and fill up the structure */
  1376.       idx = 0;
  1377.       for (sy = p->symbol_list_head;
  1378.        sy;
  1379.        sy = sy->next_in_ofile_list)
  1380.     {
  1381.       if (sy->type->type == coff_function_type)
  1382.         {
  1383.           int i;
  1384.           struct coff_line *l = sy->type->u.function.lines;
  1385.           for (i = 0; i < l->nlines; i++)
  1386.         {
  1387.           dln.section[idx] = sy->where->section->number;
  1388.           dln.sfn[idx] = n;
  1389.           dln.sln[idx] = l->lines[i];
  1390.           dln.from_address[idx] = l->addresses[i];
  1391.           if (idx)
  1392.             dln.to_address[idx - 1] = dln.from_address[idx];
  1393.           idx++;
  1394.         }
  1395.         }
  1396.       n++;
  1397.     }
  1398.       sysroff_swap_dln_out (file, &dln);
  1399.     }
  1400.  
  1401. #endif
  1402. #if 1
  1403.   /* Count up all the linenumbers */
  1404.  
  1405.   struct coff_symbol *sy;
  1406.   int lc = 0;
  1407.   struct IT_dln dln;
  1408.  
  1409.   int idx;
  1410.  
  1411.   for (sy = sfile->scope->vars_head;
  1412.        sy;
  1413.        sy = sy->next)
  1414.     {
  1415.       struct coff_type *t = sy->type;
  1416.       if (t->type == coff_function_type)
  1417.     {
  1418.       struct coff_line *l = t->u.function.lines;
  1419.       if (l)
  1420.         lc += l->nlines;
  1421.     }
  1422.     }
  1423.  
  1424.   dln.sfn = nints (lc);
  1425.   dln.sln = nints (lc);
  1426.   dln.cc = nints (lc);
  1427.   dln.section = nints (lc);
  1428.  
  1429.   dln.from_address = nints (lc);
  1430.   dln.to_address = nints (lc);
  1431.  
  1432.  
  1433.   dln.neg = 0x1001;
  1434.  
  1435.   dln.nln = lc;
  1436.  
  1437.   /* Run through once more and fill up the structure */
  1438.   idx = 0;
  1439.   for (sy = sfile->scope->vars_head;
  1440.        sy;
  1441.        sy = sy->next)
  1442.     {
  1443.       if (sy->type->type == coff_function_type)
  1444.     {
  1445.       int i;
  1446.       struct coff_line *l = sy->type->u.function.lines;
  1447.       if (l)
  1448.         {
  1449.           int base = find_base (sfile, sy->where->section);
  1450.           for (i = 0; i < l->nlines; i++)
  1451.         {
  1452.           dln.section[idx] = sy->where->section->number;
  1453.           dln.sfn[idx] = 0;
  1454.           dln.sln[idx] = l->lines[i];
  1455.           dln.from_address[idx] =
  1456.             l->addresses[i] + sy->where->section->address - base;
  1457.           dln.cc[idx] = 0;
  1458.           if (idx)
  1459.             dln.to_address[idx - 1] = dln.from_address[idx];
  1460.           idx++;
  1461.  
  1462.         }
  1463.           dln.to_address[idx - 1] = dln.from_address[idx - 1] + 2;
  1464.         }
  1465.     }
  1466.     }
  1467.   if (lc)
  1468.     sysroff_swap_dln_out (file, &dln);
  1469. #endif
  1470. }
  1471.  
  1472. /* Write the global symbols out to the debug info */
  1473. static void
  1474. wr_globals (p, sfile, n)
  1475.      struct coff_ofile *p;
  1476.      struct coff_sfile *sfile;
  1477.      int n;
  1478. {
  1479.   struct coff_symbol *sy;
  1480.   for (sy = p->symbol_list_head;
  1481.        sy;
  1482.        sy = sy->next_in_ofile_list)
  1483.     {
  1484.       if (sy->visible->type == coff_vis_ext_def
  1485.       || sy->visible->type == coff_vis_ext_ref)
  1486.     {
  1487.       /* Only write out symbols if they belong to
  1488.          the current source file */
  1489.       if (sy->sfile == sfile)
  1490.         walk_tree_symbol (sfile, 0, sy, 0);
  1491.  
  1492.     }
  1493.     }
  1494. }
  1495.  
  1496. static void
  1497. wr_debug (p)
  1498.      struct coff_ofile *p;
  1499. {
  1500.   struct coff_sfile *sfile;
  1501.   int n = 0;
  1502.   for (sfile = p->source_head;
  1503.        sfile;
  1504.        sfile = sfile->next)
  1505.  
  1506.     {
  1507.       if (debug)
  1508.     {
  1509.       printf ("%s\n", sfile->name);
  1510.     }
  1511.       wr_du (p, sfile, n);
  1512.       wr_dus (p, sfile);
  1513.       wr_program_structure (p, sfile);
  1514.       wr_dln (p, sfile, n);
  1515.       n++;
  1516.     }
  1517. }
  1518. static void
  1519. wr_cs ()
  1520. {
  1521.   /* It seems that the CS struct is not normal - the size is wrong
  1522.      heres one I prepared earlier.. */
  1523.   static char b[] =
  1524.   {0x80, 0x21, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x80, 0x80,
  1525.    0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x00,
  1526.    0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0xde};
  1527.   fwrite (b, 1, sizeof (b), file);
  1528. }
  1529.  
  1530. /* Write out the SC records for a unit.  Create an SC
  1531.    for all the sections which appear in the output file, even
  1532.    if there isn't an equivalent one on the input */
  1533.  
  1534. static void
  1535. wr_sc (ptr, sfile)
  1536.      struct coff_ofile *ptr;
  1537.      struct coff_sfile *sfile;
  1538. {
  1539.   int i;
  1540.   /* First work out the total number of sections */
  1541.  
  1542.   int total_sec = ptr->nsections;
  1543.  
  1544.   struct myinfo
  1545.     {
  1546.       struct coff_section *sec;
  1547.       struct coff_symbol *symbol;
  1548.     } myinfo;
  1549.   struct coff_symbol *symbol;
  1550.  
  1551.   struct myinfo *info
  1552.   = (struct myinfo *) calloc (total_sec, sizeof (struct myinfo));
  1553.  
  1554.  
  1555.  
  1556.   for (i = 0; i < total_sec; i++)
  1557.     {
  1558.       info[i].sec = ptr->sections + i;
  1559.       info[i].symbol = 0;
  1560.     }
  1561.  
  1562.   for (symbol = sfile->scope->vars_head;
  1563.        symbol;
  1564.        symbol = symbol->next)
  1565.     {
  1566.  
  1567.       if (symbol->type->type == coff_secdef_type)
  1568.     {
  1569.       for (i = 0; i < total_sec; i++)
  1570.         {
  1571.           if (symbol->where->section == info[i].sec)
  1572.         {
  1573.           info[i].symbol = symbol;
  1574.           break;
  1575.         }
  1576.         }
  1577.     }
  1578.     }
  1579.  
  1580.   /* Now output all the section info, and fake up some stuff for sections
  1581.      we don't have */
  1582.  
  1583.   for (i = 1; i < total_sec; i++)
  1584.     {
  1585.       struct IT_sc sc;
  1586.       char *name;
  1587.       symbol = info[i].symbol;
  1588.       sc.spare = 0;
  1589.       sc.spare1 = 0;
  1590.       if (!symbol)
  1591.     {
  1592.       /* Don't have a symbol set aside for this section, which means that nothing
  1593.          in this file does anything for the section. */
  1594.       sc.format = !(abfd->flags & EXEC_P);
  1595.       sc.addr = 0;
  1596.       sc.length = 0;
  1597.       name = info[i].sec->name;
  1598.     }
  1599.       else
  1600.     {
  1601.       unsigned int low = symbol->where->offset;
  1602.       unsigned int high = symbol->where->offset + symbol->type->size - 1;
  1603.  
  1604.       if (abfd->flags & EXEC_P)
  1605.         {
  1606.           sc.format = 0;
  1607.           sc.addr = symbol->where->offset;
  1608.         }
  1609.       else
  1610.         {
  1611.           sc.format = 1;
  1612.           sc.addr = 0;
  1613.         }
  1614.       sc.length = symbol->type->size;
  1615.       name = symbol->name;
  1616.     }
  1617.  
  1618.       sc.align = 4;
  1619.  
  1620.       sc.concat = CONCAT_SIMPLE;
  1621.       sc.read = 3;
  1622.       sc.write = 3;
  1623.       sc.exec = 3;
  1624.       sc.init = 3;
  1625.       sc.mode = 3;
  1626.       sc.spare = 0;
  1627.       sc.segadd = 0;
  1628.       sc.spare1 = 0;        /* If not zero, then it doesn't work */
  1629.       sc.name = section_translate (name);
  1630.       if (strlen (sc.name) == 1)
  1631.     {
  1632.       switch (sc.name[0])
  1633.         {
  1634.         case 'D':
  1635.         case 'B':
  1636.           sc.contents = CONTENTS_DATA;
  1637.           break;
  1638.         default:
  1639.           sc.contents = CONTENTS_CODE;
  1640.         }
  1641.     }
  1642.       else
  1643.     {
  1644.       sc.contents = CONTENTS_CODE;
  1645.     }
  1646.  
  1647.  
  1648.       sysroff_swap_sc_out (file, &sc);
  1649.  
  1650.  
  1651.  
  1652.     }
  1653.  
  1654. }
  1655.  
  1656. static void
  1657. wr_er (ptr, sfile, first)
  1658.      struct coff_ofile *ptr;
  1659.      struct coff_sfile *sfile;
  1660.      int first;
  1661. {
  1662.   int idx = 0;
  1663.   struct coff_symbol *sym;
  1664.   if (first)
  1665.     {
  1666.       for (sym = ptr->symbol_list_head; sym; sym = sym->next_in_ofile_list)
  1667.     {
  1668.       if (sym->visible->type == coff_vis_ext_ref)
  1669.         {
  1670.           struct IT_er er;
  1671.           er.spare = 0;
  1672.           er.type = ER_NOTSPEC;
  1673.           er.name = sym->name;
  1674.           sysroff_swap_er_out (file, &er);
  1675.           sym->er_number = idx++;
  1676.  
  1677.         }
  1678.     }
  1679.     }
  1680. }
  1681.  
  1682. static void
  1683. wr_ed (ptr, sfile, first)
  1684.      struct coff_ofile *ptr;
  1685.      struct coff_sfile *sfile;
  1686.      int first;
  1687. {
  1688.   struct coff_symbol *s;
  1689.   if (first)
  1690.     {
  1691.       for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
  1692.     {
  1693.       if (s->visible->type == coff_vis_ext_def
  1694.           || s->visible->type == coff_vis_common)
  1695.         {
  1696.           struct IT_ed ed;
  1697.  
  1698.           ed.section = s->where->section->number;
  1699.           ed.spare = 0;
  1700.           if (s->where->section->data)
  1701.         {
  1702.           ed.type = ED_TYPE_DATA;
  1703.         }
  1704.           else if (s->where->section->code & SEC_CODE)
  1705.         {
  1706.           ed.type = ED_TYPE_ENTRY;
  1707.         }
  1708.           else
  1709.         {
  1710.           ed.type = ED_TYPE_NOTSPEC;
  1711.           ed.type = ED_TYPE_DATA;
  1712.         }
  1713.           ed.address = s->where->offset - s->where->section->address;
  1714.           ed.name = s->name;
  1715.           sysroff_swap_ed_out (file, &ed);
  1716.         }
  1717.     }
  1718.     }
  1719. }
  1720.  
  1721. static void
  1722. wr_unit_info (ptr)
  1723.      struct coff_ofile *ptr;
  1724. {
  1725.   struct coff_sfile *sfile;
  1726.   int first = 1;
  1727.   for (sfile = ptr->source_head;
  1728.        sfile;
  1729.        sfile = sfile->next)
  1730.     {
  1731.       wr_un (ptr, sfile, first);
  1732.       wr_sc (ptr, sfile);
  1733.       wr_er (ptr, sfile, first);
  1734.       wr_ed (ptr, sfile, first);
  1735.       first = 0;
  1736.     }
  1737. }
  1738.  
  1739. static void
  1740. wr_module (p)
  1741.      struct coff_ofile *p;
  1742. {
  1743.   wr_cs ();
  1744.   wr_hd (p);
  1745.   wr_unit_info (p);
  1746.   wr_object_body (p);
  1747.   wr_debug (p);
  1748.   wr_tr ();
  1749. }
  1750.  
  1751. static int
  1752. align (x)
  1753.      int x;
  1754. {
  1755.   return (x + 3) & ~3;
  1756. }
  1757.  
  1758. /* Find all the common variables and turn them into
  1759.    ordinary defs - dunno why, but thats what hitachi does with 'em */
  1760.  
  1761. static void
  1762. prescan (tree)
  1763.      struct coff_ofile *tree;
  1764. {
  1765.   struct coff_symbol *s;
  1766.   struct coff_section *common_section;
  1767.   /* Find the common section - always section 3 */
  1768.   common_section = tree->sections + 3;
  1769.   for (s = tree->symbol_list_head;
  1770.        s;
  1771.        s = s->next_in_ofile_list)
  1772.     {
  1773.       if (s->visible->type == coff_vis_common)
  1774.     {
  1775.       struct coff_where *w = s->where;
  1776.       /*      s->visible->type = coff_vis_ext_def; leave it as common */
  1777.       common_section->size = align (common_section->size);
  1778.       w->offset = common_section->size + common_section->address;
  1779.       w->section = common_section;
  1780.       common_section->size += s->type->size;
  1781.       common_section->size = align (common_section->size);
  1782.     }
  1783.     }
  1784. }
  1785.  
  1786. char *program_name;
  1787.  
  1788. static void
  1789. show_usage (file, status)
  1790.      FILE *file;
  1791.      int status;
  1792. {
  1793.   fprintf (file, "Usage: %s [-dhVq] in-file [out-file]\n", program_name);
  1794.   exit (status);
  1795. }
  1796.  
  1797. static void
  1798. show_help ()
  1799. {
  1800.   printf ("%s: Convert a COFF object file into a SYSROFF object file\n",
  1801.       program_name);
  1802.   show_usage (stdout, 0);
  1803. }
  1804.  
  1805.  
  1806.  
  1807. int
  1808. main (ac, av)
  1809.      int ac;
  1810.      char *av[];
  1811. {
  1812.   int opt;
  1813.   static struct option long_options[] =
  1814.   {
  1815.     {"debug", no_argument, 0, 'd'},
  1816.     {"quick", no_argument, 0, 'q'},
  1817.     {"help", no_argument, 0, 'h'},
  1818.     {"version", no_argument, 0, 'V'},
  1819.     {NULL, no_argument, 0, 0}
  1820.   };
  1821.   char **matching;
  1822.   char *input_file;
  1823.  
  1824.   char *output_file;
  1825.   program_name = av[0];
  1826.   xmalloc_set_program_name (program_name);
  1827.  
  1828.   while ((opt = getopt_long (ac, av, "dhVq", long_options,
  1829.                  (int *) NULL))
  1830.      != EOF)
  1831.     {
  1832.       switch (opt)
  1833.     {
  1834.     case 'q':
  1835.       quick = 1;
  1836.       break;
  1837.     case 'd':
  1838.       debug = 1;
  1839.       break;
  1840.     case 'h':
  1841.       show_help ();
  1842.       /*NOTREACHED*/
  1843.     case 'V':
  1844.       printf ("GNU %s version %s\n", program_name, PROGRAM_VERSION);
  1845.       exit (0);
  1846.       /*NOTREACHED*/
  1847.     case 0:
  1848.       break;
  1849.     default:
  1850.       show_usage (stderr, 1);
  1851.       /*NOTREACHED*/
  1852.     }
  1853.     }
  1854.  
  1855.   /* The input and output files may be named on the command line.  */
  1856.   output_file = NULL;
  1857.   if (optind < ac)
  1858.     {
  1859.       input_file = av[optind];
  1860.       ++optind;
  1861.       if (optind < ac)
  1862.     {
  1863.       output_file = av[optind];
  1864.       ++optind;
  1865.       if (optind < ac)
  1866.         show_usage (stderr, 1);
  1867.       if (strcmp (input_file, output_file) == 0)
  1868.         {
  1869.           fprintf (stderr,
  1870.                "%s: input and output files must be different\n",
  1871.                program_name);
  1872.           exit (1);
  1873.         }
  1874.     }
  1875.     }
  1876.   else
  1877.     input_file = 0;
  1878.  
  1879.   if (!input_file)
  1880.     {
  1881.       fprintf (stderr, "%s: no input file specified\n",
  1882.            program_name);
  1883.       exit (1);
  1884.     }
  1885.  
  1886.   if (!output_file)
  1887.     {
  1888.       /* Take a .o off the input file and stick on a .obj.  If
  1889.      it doesn't end in .o, then stick a .obj on anyway */
  1890.  
  1891.       int len = strlen (input_file);
  1892.       output_file = xmalloc (len + 5);
  1893.       strcpy (output_file, input_file);
  1894.       if (len > 3
  1895.       && output_file[len - 2] == '.'
  1896.       && output_file[len - 1] == 'o')
  1897.     {
  1898.       output_file[len] = 'b';
  1899.       output_file[len + 1] = 'j';
  1900.       output_file[len + 2] = 0;
  1901.     }
  1902.       else
  1903.     {
  1904.       strcat (output_file, ".obj");
  1905.     }
  1906.     }
  1907.  
  1908.   abfd = bfd_openr (input_file, 0);
  1909.  
  1910.   if (!abfd)
  1911.     bfd_fatal (input_file);
  1912.  
  1913.   if (!bfd_check_format_matches (abfd, bfd_object, &matching))
  1914.     {
  1915.       bfd_nonfatal (input_file);
  1916.       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  1917.     {
  1918.       list_matching_formats (matching);
  1919.       free (matching);
  1920.     }
  1921.       exit (1);
  1922.     }
  1923.  
  1924.   file = fopen (output_file, "wb");
  1925.  
  1926.   if (!file)
  1927.     {
  1928.       fprintf (stderr, "%s: unable to open output file %s\n",
  1929.            program_name, output_file);
  1930.       exit (1);
  1931.     }
  1932.  
  1933.   if (debug)
  1934.     printf ("ids %d %d\n", base1, base2);
  1935.   tree = coff_grok (abfd);
  1936.   prescan (tree);
  1937.   wr_module (tree);
  1938.   return 0;
  1939. }
  1940.